#include "selectiontypemenu.h"
#include "ui_selectiontypemenu.h"
#include "keybindings.h"
#include "removablemedia.h"

SelectionTypeMenu::SelectionTypeMenu(QWidget *parent, FileSelectionInterface * fsi) :
    KeyControllableMenu(parent),
    m_ui(new Ui::SelectionTypeMenu)
{
    m_ui->setupUi(this);
    WindowManager::addWidget(this);
    WindowManager::addWidget(fsi->getInterface());
    setMouseTracking(true);
    selector = new FileSelector(this,fsi);
    folderList = new FolderList(this,selector);
    playlistDisplay = new PlaylistDisplay(this,selector);
    connect(selector, SIGNAL(keyPressed(QKeyEvent*)),this,SLOT(keyPressEvent(QKeyEvent*)));
    //setStyleSheets("border:0px; background-color: red; color: white; font-size 40px;","border:0px; color: white; ");
    addButton(":/imgs/3F00Button_ViewAll.png");
    favouritesButton= addButton(":/imgs/3F00Button_Favourites.png");
    playlistButton = addButton(":/imgs/3F00Button_Playlist.png");
    folderButton = addButton(":/imgs/3F00Button_Folder.png");
    //removableButton= addButton(":/imgs/3F00Button_RemovableMedia.png");
   // RemovableMedia * media =new RemovableMedia(this);
    //media->hide();
    //removableButton->setItem(media);
}

SelectionTypeMenu::~SelectionTypeMenu()
{
    delete m_ui;
}

void SelectionTypeMenu::buttonSelected() {
    KeyControllableMenu::buttonSelected();
    //m_ui->widget1->hide();
    //qDebug() <<"selected";
    if(getCurrentButton()==favouritesButton) {
        //qDebug() <<"favourites clicked";
        selector->setFilter("mf.favourite = '1'");
        selector->reloadFiles();
    }/* else if (getCurrentButton()==removableButton) {
        RemovableMedia * media=qobject_cast<RemovableMedia *>(removableButton->getItem());
        if(media) {
            media->show();
        }
    }*/
    else {
        selector->setFilter("");
        selector->reloadFiles();
    }
    if(getCurrentButton() == playlistButton){
        playlistDisplay->reloadPlaylists();
        WindowManager::showWidget(playlistDisplay);
    }
    else if(getCurrentButton() == folderButton){
        folderList->reloadFolders();
        WindowManager::showWidget(folderList);
    }
    else if((FileSelector *)getCurrentButton()->getItem()==selector) {
        qDebug()<<"showing selector";
        WindowManager::showWidget(selector);
    }
}

void SelectionTypeMenu::show() {
   KeyControllableMenu::show();
   selector->hide();
   m_ui->widget1->show();
}

Button * SelectionTypeMenu::addButton(QString icon) {
    Button * button =new Button(QIcon(icon),NULL, this, (MenuItemInterface *)selector);
    button->setIconSize(QSize(256,64));
    QList<Button *> * currentList=new QList<Button *>();
    currentList->append(button);
    addRow(currentList);
    m_ui->verticalLayout->addWidget(button);
    return button;
}

